package com.beautify.service.impl;

import com.beautify.entity.pojo.Result;
import com.beautify.entity.pojo.User;
import com.beautify.mapper.UserMapper;
import com.beautify.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public Result login(String username, String password) {
        User users = userMapper.login(username, password);
        if (users == null)
            return Result.error("登录失败");
        else
            return Result.success(users);
    }

    public Result register(Integer types, String username, String password) {
        User users = userMapper.findByName(username);
        if (users != null)
            return Result.error("注册失败,用户名已存在");
        else {
            userMapper.register(types, username, password);
            return Result.success();
        }

    }

    public Result deleteUser(Integer userid) {
        int deleteusers = userMapper.deleteUser(userid);
        if (deleteusers == 1)
            return Result.success();
        else
            return Result.error("注销失败");
    }

    public Result updateUser(User user) {
        userMapper.updateUser(user);
        return Result.success();
    }

    public Result updatePassword(String oldpassword, String newpassword, Integer userid) {
        int userpasswod = userMapper.updatePassword(oldpassword, newpassword, userid);
        if (userpasswod == 0)
            return Result.error("原密码不正确");
        else
            return Result.success();
    }

    public Result getById(Integer userid) {
        User user = userMapper.getById(userid);
        if (user != null)
            return Result.success(user);
        else
            return Result.error("查询失败");
    }

    public User getByIdToUser(Integer userid) {
        return userMapper.getById(userid);
    }

    @Override
    public Result getDeviceIdentifier(){
        String sbs=DeviceIdentifier();
        Random random = new Random();
        Integer id = 0;
        int randomNumber = 0;

//        System.out.println("-----------------");
//        System.out.println("sbs = " + sbs);
//        System.out.println("-----------------");
        if(sbs!=null) {
           id =  Integer.valueOf(sbs);
        }
        else{

            for(int i = 0; i<1000; i++) {
                randomNumber = random.nextInt(10000) + 1000;
                User byId = userMapper.getById(randomNumber);
                if (byId == null) {
                    break;
                }
            }

        }

        if(sbs != null && !sbs.isEmpty()){
            // 用户再次注册
            User byId = userMapper.getById(Integer.valueOf(sbs));
            if(byId!=null){
                for (int i = 0; i < 1000; i++) {
                    randomNumber = random.nextInt(10000) + 1000;
                    if (userMapper.getById(randomNumber) == null){
                        break;
                    }
                }
                userMapper.insertid(0,randomNumber);

                User userMapperById = userMapper.getById(randomNumber);
                return Result.success(9999,userMapperById);
            }else{
                userMapper.insertid(0,id);

                User userMapperById = userMapper.getById(id);
                return Result.success(userMapperById);
            }
        }else{
            // 获取不到MAC地址
            userMapper.insertid(0,randomNumber);

            User userMapperById = userMapper.getById(randomNumber);
            return Result.success(9999,userMapperById);
        }

    }

    public String DeviceIdentifier() {
        try {
            // 获取网络接口的MAC地址
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());

            for (NetworkInterface intf : interfaces) {
                if (intf.getName().equalsIgnoreCase("eth0")) { // 根据实际接口名称进行选择
                    byte[] mac = intf.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (byte b : mac) {
                            sb.append(String.format("%02X:",b));
                        }
                        if (sb.length() > 0) {
                            sb.deleteCharAt(sb.length() - 1); // 移除最后一个冒号
                        }

                        //String sbs=sb.toString().replace(":", "");
                        String sbs=sb.toString().replaceAll("[^0-9]", "");
                        return sbs;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

}
